package com.scau.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.scau.pojo.EWallet;
import com.scau.service.IEWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@CrossOrigin
@RequestMapping("")
public class EWalletLogin {
    @Autowired
    IEWalletService service;

    @RequestMapping(value="/wallet/toLoginPage",produces = "application/json;charset=UTF-8;")
//    @ResponseBody
    public String toSignPage(){
        return "EWalletLogin";
    }



    //判断username、password和数据库是否一致
    @RequestMapping(value = "/wallet/login",method = RequestMethod.POST)
    @ResponseBody
    public String login(EWallet eWallet, Model model) throws JsonProcessingException {


        EWallet temp = service.getWallet(eWallet.getPayId());
        System.out.println(temp);
        if (temp == null) {
            model.addAttribute("msg", "该用户不存在");

        } else if (temp.getPassword().equals(eWallet.getPassword())) {
            model.addAttribute("msg", "登录成功");
            System.out.println(temp);
            ObjectMapper objectMapper = new ObjectMapper();
            String json = objectMapper.writeValueAsString(temp);
            return json;
        }
        //与数据库不一致，返回登录页面
        String msg = "error";
        return msg;

    }

}
